<template>
  <div class="app-container">
    <el-form :inline="true" ref="refForm" :model="form">
      <el-form-item label="账号" prop="username">
         <el-input v-model="form.username" placeholder="请输入账号" @keyup.enter.native="submitFilterForm"></el-input>
      </el-form-item>
      <el-form-item label="获奖时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="粽子类型" prop="zongziType">
        <el-select v-model="form.zongziType" placeholder="请选择粽子类型">
          <el-option label="全部" key="" value=""></el-option>
          <el-option label="如意粽子" key="0" value='0'></el-option>
          <el-option label="吉祥粽子" key="1" value='1'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="奖品类型" prop="rewardType">
        <el-select v-model="form.rewardType" placeholder="请选择奖品类型">
          <el-option label="全部" key="" value=""></el-option>
          <el-option label="实物" key="0" value='0'></el-option>
          <el-option label="礼金" key="1" value='1'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="奖品" prop="level">
        <el-select v-model="form.rewardId" placeholder="请选择奖品" :disabled="rewardDisableFlag">
          <el-option label="全部" value=""></el-option>
          <el-option v-for="(item,index) in rewardListFinal" :label="item.name" :key="'rewardList'+index" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
        <el-button @click="selfResetForm('refForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      :data="tableData.list"
      v-loading="loading"
      element-loading-text="加载中..."
      class="el-table-align-center">
      <el-table-column prop="username" label="账号" align="center"></el-table-column>
      <el-table-column prop="createTime" label="获奖时间" align="center">
        <template slot-scope="{row}">
          {{row.createTime | toTime('YYYY-MM-DD HH:mm:ss')}}
        </template>
      </el-table-column>
      <el-table-column prop="zongziType" label="粽子类型" align="center">
        <template slot-scope="{row}">
          {{row.zongziType===0?'如意粽子':'吉祥粽子'}}
        </template>
      </el-table-column>
      <el-table-column prop="rewardType" label="奖品类型" align="center">
        <template slot-scope="{row}">
          {{row.rewardType===0?'实物':'礼金'}}
        </template>
      </el-table-column>
      <el-table-column prop="reward" label="奖品" align="center"></el-table-column>
      <el-table-column prop="remarks" label="备注" align="center"></el-table-column>
      <el-table-column prop="operate" label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="text" @click="openRemark(row)">备注</el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-dialog title="备注" :visible.sync="operateDialogFlag" append-to-body width="30%">
      <remark-dialog @refresh="refresh" :rowData="rowData" v-if='operateDialogFlag' v-model="operateDialogFlag"></remark-dialog>
    </el-dialog>

    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { grid } from '@/build';
import remarkDialog from './remarkDialog';
import { getRiceDumplingList } from '@/services/api/activity';
import rewardList from './reward';

export default {
  name: 'list',
  components: { remarkDialog },
  mixins: [grid],
  data() {
    const time = [moment().format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')];
    const startTime = time[0];
    const endTime = time[1];
    return {
      tableData: {
        list: [],
        total: 0,
      },
      form: {
        time,
        username: '',
        zongziType: '',
        rewardType: '',
        rewardId: '',
      },
      filters: {
        startTime,
        endTime,
        username: '',
        zongziType: '',
        rewardType: '',
        rewardId: '',
        size: 20,
        page: 1,
      },
      rewardList: [],
      rewardListFinal: [],
      operateDialogFlag: false,
      rewardDisableFlag: true,
      rowData: undefined,
    };
  },
  mounted() {
    this.bindGet(getRiceDumplingList, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
      beforeSend: (data) => {
        delete data.time;
        return data;
      },
    });
    this.getList();
  },
  watch: {
    'form.zongziType': function (val) {
      this.form.rewardId = '';
      if (val === '') {
        this.rewardDisableFlag = true;
      } else {
        this.rewardDisableFlag = false;
        this.rewardListFinal = this.rewardList = rewardList[val];
        if (this.form.rewardType !== '') {
          this.rewardListFinal = this.rewardList.filter(item => item.rewardType === Number(this.form.rewardType));
        }
      }
    },
    'form.rewardType': function (val) {
      this.form.rewardId = '';
      if (val === '') {
        this.rewardListFinal = this.rewardList;
      } else {
        this.rewardListFinal = this.rewardList.filter(item => item.rewardType === Number(val));
      }
    },
  },
  methods: {
    getList() {
      this.doGet({ action: 'getGrid' });
    },
    submitFilterForm() {
      const { time } = this.form;
      let startTime;
      let endTime;
      if (!_.isUndefined(time) && _.isArray(time)) {
        startTime = time[0];
        endTime = time[1];
      }
      this.filters = {
        ...this.filters, ...this.form, startTime, endTime,
      };
      this.gridSearch();
    },
    selfResetForm(formName) {
      this.resetForm(formName);
      this.submitFilterForm();
    },
    openRemark(row) {
      this.rowData = row;
      this.operateDialogFlag = true;
    },
    refresh() {
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
